package com.zq.daoImpl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.zq.dao.BaseDao_zq;
import com.zq.dao.EvaDao;
import com.zq.page.Page;
import com.zq.pojo.Eva;
@Repository("evaDao")
public class EvaDaoImpl extends BaseDao_zq implements EvaDao {
	/*private SessionFactory sessionFactory;
	public EvaDaoImpl() {
		sessionFactory = HibernateSessionFactory.getSessionFactory();
	}*/
	//增
	@Override
	public void add(Eva eva) {
		Session session = getSession();
		session.save(eva);
		session.close();
	}
	//删
	@Override
	public void delete(Eva eva) {
		Session session = getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(eva);
		transaction.commit();
		session.close();
	}
	//改
	@Override
	public void update(Eva eva) {
		Session session = getSession();
		Transaction transaction = session.beginTransaction();
		session.update(eva);
		transaction.commit();
		session.close();
	}
	//查一
	@Override
	public Eva findById(int eid) {
		Session session = getSession();
		String hql = "from Eva where eid=?";
		Eva eva = (Eva) session.createQuery(hql).setParameter(0, eid).uniqueResult();
		session.close();
		return eva;
	}
	//查所有
	@SuppressWarnings("unchecked")
	@Override
	public List<Eva> findAll() {
		Session session = getSession();
		String hql = "from Eva";
		List<Eva> evas = session.createQuery(hql).list();
		session.close();
		return evas;
	}

	
	//根据empno查找对应的该员工部门编号
	@SuppressWarnings("unchecked")
	@Override
	public List<Eva> getManyThings() {
		Session session = getSession();
		String sql = "SELECT l.eid,j.empno,j.name,j.deptname,l.attendance,l.skill,l.attitude,l.achievement FROM t_eva  l INNER JOIN(SELECT e.name,d.deptname,e.empno FROM t_emp e INNER JOIN t_dept d ON d.deptno = e.intro) j  ON l.empno = j.empno";
		List<Eva> evas = session.createSQLQuery(sql).addEntity(Eva.class).list();
		session.close();
		return evas;
		
		
		/*Connection conn = JDBCTools.getConnection();
		String sql = "SELECT j.empno,j.name,j.deptname,l.attendance,l.skill,l.attitude,l.achievement FROM t_evaluation  l INNER JOIN(SELECT e.name,d.deptname,e.empno FROM t_emp e INNER JOIN t_dept d ON d.deptno = e.intro) j  ON l.empno = j.empno";
		PreparedStatement ps =null;
		ResultSet rs = null;
		Eva eva = null;
		List<Eva> evas = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next())
			{
				int empno = rs.getInt(1);
				String name = rs.getString(2);
				String deptName = rs.getString(3);
				int attendance = rs.getInt(4);
				String skill = rs.getString(5);
				String attitude = rs.getString(6);
				String achievement = rs.getString(7);
				eva = new Eva(name, deptName, empno, attendance, skill, attitude, achievement);
				evas.add(eva);
				
			}
			return evas;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;*/
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Eva> findAll(int pageNo) throws Exception {
		Session session = getSession();
		String hql = "from Eva";
		List<Eva> evas = session.createQuery(hql).setFirstResult((pageNo - 1)* 3).setMaxResults(3).list();
		session.close();
		return evas;
	}
	@Override
	public Page<Eva> getPage(int pageNo) throws Exception {
		Page<Eva> page = new Page<>();
		page.setPageNo(pageNo);
		page.setTotalNum(getTotalNum());
		page.setList(findAll(page.getPageNo()));
		return page;
	}
	@Override
	public int getTotalNum() throws Exception {
		Session session = getSession();
		String sql = "SELECT COUNT(*) FROM t_eva";
		Object obj = session.createSQLQuery(sql).uniqueResult();
		int a = -1;
		if(obj != null){
			a = Integer.parseInt(obj.toString());
		}
		session.close();
		System.out.println("a=" + a);
		return a;
	}

}
